package rpg.engine.client;

import java.awt.Color;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import java.util.concurrent.ConcurrentHashMap;

import rpg.client.model.Actor;
import rpg.client.model.ActorInfo;
import rpg.client.model.Facing;
import rpg.client.model.Position;
import rpg.client.model.Actor.Gender;
import rpg.engine.model.DialogInfo;

public class NpcInfo {
    public static final List<NpcInfo> EMPTY_LIST = Collections.emptyList();

    private final Object LOCK = new Object();

    private Gender gender = Gender.MALE;
    private Color hairColour = Color.BLACK;

    private final String id;
    private final String name;

    private Position position;
    private final Facing facing;
    private final Map<String, List<DialogInfo>> mapStoryToIntroDialogs = new ConcurrentHashMap<String, List<DialogInfo>>();

    public NpcInfo(ActorInfo a) {
        super();
        id = a.getActor().getId();
        name = a.getActor().getName();
        setGender(a.getActor().getGender());
        setHairColour(a.getActor().getHairColour());
        position = a.getPosition();
        facing = a.getFacing();
    }

    public void addIntroDialog(String storyID, DialogInfo dialogInfo) {
        List<DialogInfo> list = mapStoryToIntroDialogs.get(storyID);
        if (list == null) {
            list = new Vector<DialogInfo>();
            mapStoryToIntroDialogs.put(storyID, list);
        }

        list.add(dialogInfo);
    }

    public Actor getActor() {
        synchronized (LOCK) {
            Actor.Builder ab = new Actor.Builder();
            ab.gender(getGender());
            ab.id(id);
            ab.name(name);
            ab.hairColour(getHairColour());
            return ab.build();
        }
    }

    public Facing getFacing() {
        synchronized (LOCK) {
            return facing;
        }
    }

    private Gender getGender() {
        synchronized (LOCK) {
            return gender;
        }
    }

    private Color getHairColour() {
        synchronized (LOCK) {
            return hairColour;
        }
    }

    public Collection<DialogInfo> getIntroDialogs(String storyID) {
        return mapStoryToIntroDialogs.get(storyID);
    }

    public Position getPosition() {
        synchronized (LOCK) {
            return position;
        }
    }

    public void setGender(Gender g) {
        synchronized (LOCK) {
            gender = g;
        }
    }

    private void setHairColour(Color hairColour2) {
        synchronized (LOCK) {
            hairColour = hairColour2;
        }
    }

    public void setPosition(Position p) {
        synchronized (LOCK) {
            position = p;
        }
    }
}
